<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 产品出库单
 *
 * @icon fa fa-circle-o
 */
class ClothmakingOutstock extends Backend
{

    /**
     * ClothmakingOutstock模型对象
     * @var \app\admin\model\ClothmakingOutstock
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\ClothmakingOutstock;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("outTypeList", $this->model->getOutTypeList());
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model->with(['product', 'operator'])
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    public function selectpage()
    {
        // 设置过滤方法
        $this->request->filter(['strip_tags', 'htmlspecialchars']);

        // 当前页
        $page = $this->request->request("pageNumber", 1);
        // 分页大小
        $pagesize = $this->request->request("pageSize", 10);
        $search = $this->request->get('search', ''); // 搜索关键字
        // 主键
        $primarykey = $this->request->request("keyField", 'id');
        $custom = (array)$this->request->request("custom/a");
        // 查询条件
        $where = [];
        if ($search) {
            $where[] = ['title', 'like', '%' . $search . '%']; // 根据 title 字段搜索
        }
// data-params='{"custom[status]":"checked"}'
         // 添加自定义搜索条件
         foreach ($custom as $k => $v) {
            if (is_array($v) && count($v) == 2) {
                $where[$k] = [$v[0], $v[1]];
            } else {
                $where[$k] = $v;
            }
        }

        // 查询数据
        $list = $this->model->with('product')->where($where)
            
            ->order($primarykey, 'desc')
            ->page($page, $pagesize)
            ->select();

             // 获取数据总数
        $total = $this->model->where($where)->count();
        // 格式化数据
        return json(['list' => $list, 'total' => $total]);
    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
